import {makeAutoObservable, runInAction} from 'mobx'
import { reqSearchUsers } from '../api'
import { Users } from '@/api/types'

class UserSearchStore {
  firstView: boolean = true
  loading: boolean = false
  errorMsg: string = ''
  users: Users = []

  constructor () {
    makeAutoObservable(this)
  }

  async search (keyword: string) {
     // 发请求前, 更新数据
     this.firstView = false
     this.loading = true

     try {
       // 发请求获取数据
       const result = await reqSearchUsers(keyword)
       // login / avatar_url / html_url
       // 读取数据
       const users = result.items
       runInAction(() => {
          // 更新
        this.loading = false
        this.users = users
       })
     } catch (error: any) {
       runInAction(() => {
        // 更新
        this.loading = false
        this.errorMsg = error.message
       })
     }
  }
}

export default new UserSearchStore()